﻿using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace slwhTools.IConverter
{
    public class PdfConverterService:IConverter
    {
        // PDF转Word
        public static void ConvertPdfToWord(string pdfPath, string wordPath)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(pdfPath);
            doc.SaveToFile(wordPath, FileFormat.DOCX);
        }

        // PDF转图片（每页一张图片）
        public static void ConvertPdfToImages(string pdfPath, string outputFolder, ImageFormat imageFormat = null)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(pdfPath);

            imageFormat = imageFormat ?? ImageFormat.Png;

            // 将每一页保存为图片
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                using (Image image = doc.SaveAsImage(i))
                {
                    string ext = imageFormat == ImageFormat.Jpeg ? "jpg" : "png";
                    string imagePath = Path.Combine(outputFolder, $"page_{i + 1}.{ext}");
                    image.Save(imagePath, imageFormat);
                }
            }
        }
    }
}
